import lejos.nxt.*;

public class LightDistancer {
    private int offset = 0;
    private LightSensor sensor;
    private boolean wasRead = false;
    
    public LightDistancer(SensorPort s) {
    	sensor = new LightSensor(s, true);
    	for (int i=0; i<100; i++) {
    		offset = sensor.readValue();
    	}
    }
    
    public int getData() {
    	if (wasRead) {
    		return sensor.readValue() - offset;
    	} else {
    		wasRead = true;
    		offset = sensor.readValue();
    		return 0;
    	}
    }  
}
